package com.li.youhappygo.controller;

import com.github.pagehelper.PageInfo;
import com.li.youhappygo.annotation.Log;
import com.li.youhappygo.annotation.OperatorType;
import com.li.youhappygo.mapper.BrandMapper;
import com.li.youhappygo.model.entity.commond.Brand;
import com.li.youhappygo.model.vo.common.Result;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.service.BrandService;
import io.swagger.annotations.Api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/brand")
@Tag(name = "品牌管理", description = "品牌管理")
public class BrandController {
    @Autowired
    BrandService brandService;

    @GetMapping("/getBrandList/{pageNum}/{pageSize}")
    @Operation(description = "显示所有品牌信息，分页查询",summary = "查询所有品牌")
    @Transactional
    public Result<PageInfo<Brand>> getBrandList(@Parameter(required = true, description = "页码")@PathVariable Integer pageNum,@Parameter(required = true, description = "每页显示数量") @PathVariable Integer pageSize) {
        List<Brand> brandList =brandService.getBrandList(pageNum, pageSize);
        System.out.println("11111111111111111111");
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);

        return Result.build(pageInfo, ResultCode.SUCCESS);
    }
    @GetMapping("/getBrandById/{brandId}")
    @Operation(summary = "查找有一个品牌",description = "根据品牌ID一个品牌信息")
    public Result<Brand> getBrandById(@Parameter(required = true, description = "品牌id") @PathVariable Long brandId){
        Brand brand = brandService.getBrandByID(brandId);
        return Result.build(brand, ResultCode.SUCCESS);
    }

    @PostMapping("save")
    @Operation(description = "插入一个品牌",summary = "插入品牌")
    @Log(title = "品牌插入", businessType = 1,operatorType = OperatorType.MANAGE)
    public Result save(@Parameter(description = "传入需要插入的Brand信息" ,required = true ,schema = @Schema(implementation = Brand.class))@RequestBody Brand brand) {
        brandService.save(brand);
        return Result.build(null , ResultCode.SUCCESS) ;
    }

    @PutMapping("updateById")
    @Operation(description = "更新一个品牌",summary = "更新品牌信息")
    @Log(title = "更新一个品牌", businessType = 2,operatorType = OperatorType.MANAGE)
    public Result updateById(@Parameter(required = true, description = "需要更新的品牌信息",schema = @Schema(implementation = Brand.class))@RequestBody Brand brand) {
        brandService.updateById(brand);
        return Result.build(null , ResultCode.SUCCESS) ;
    }

    @DeleteMapping("/deleteById/{id}")
    @Operation(description = "根据品牌ID删除一个品牌",summary = "删除品牌")
    @Log(title = "根据品牌ID删除一个品牌",businessType = 3,operatorType = OperatorType.MANAGE)
    public Result deleteById(@Parameter(required = true, description = "需要删除的品牌ID") @PathVariable Long id) {
        brandService.deleteById(id);
        return Result.build(null , ResultCode.SUCCESS) ;
    }

}
